EDA(exploratory data analysis)

World Bank에서 발표한 세계 여러 나라의 각종 지표 데이터를 이용하여 EDA를 진행했습니다.

  • 각 나라의 인구, GDP, 출산률, 사망률, 문맹률, 영아사망률 등의 정보가 담긴 데이터

0. 라이브러리, 데이터 준비

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pi
import seaborn as sns
wb_df = pd.read_csv("data.csv")

1. 데이터 살펴보기

1) 각 컬럼의 내용

df.dtypes
Country Name                                                                                   object
Country Code                                                                                   object
year                                                                                            int64
Access to electricity (% of population)                                                       float64
Access to electricity, rural (% of rural population)                                          float64
Agricultural irrigated land (% of total agricultural land)                                    float64
Agricultural land (% of land area)                                                            float64
Agricultural land (sq. km)                                                                    float64
Agricultural machinery, tractors                                                              float64
Agricultural machinery, tractors per 100 sq. km of arable land                                float64
Agricultural methane emissions (% of total)                                                   float64
Agricultural methane emissions (thousand metric tons of CO2 equivalent)                       float64
Agricultural nitrous oxide emissions (% of total)                                             float64
Agricultural nitrous oxide emissions (thousand metric tons of CO2 equivalent)                 float64
Agricultural raw materials exports (% of merchandise exports)                                 float64
Agricultural raw materials imports (% of merchandise imports)                                 float64
Agriculture, forestry, and fishing, value added (% of GDP)                                    float64
Agriculture, forestry, and fishing, value added (current US$)                                 float64
Annual freshwater withdrawals, agriculture (% of total freshwater withdrawal)                 float64
Arable land (% of land area)                                                                  float64
Arable land (hectares per person)                                                             float64
Arable land (hectares)                                                                        float64
Average precipitation in depth (mm per year)                                                  float64
Birth rate, crude (per 1,000 people)                                                          float64
Cereal production (metric tons)                                                               float64
Cereal yield (kg per hectare)                                                                 float64
CPIA gender equality rating (1=low to 6=high)                                                 float64
Crop production index (2004-2006 = 100)                                                       float64
Death rate, crude (per 1,000 people)                                                          float64
Employment in agriculture (% of total employment) (modeled ILO estimate)                      float64
Employment in agriculture, female (% of female employment) (modeled ILO estimate)             float64
Employment in agriculture, male (% of male employment) (modeled ILO estimate)                 float64
Fertilizer consumption (% of fertilizer production)                                           float64
Fertilizer consumption (kilograms per hectare of arable land)                                 float64
Food production index (2004-2006 = 100)                                                       float64
Forest area (% of land area)                                                                  float64
Forest area (sq. km)                                                                          float64
GDP per capita (current US$)                                                                  float64
Land area (sq. km)                                                                            float64
Land under cereal production (hectares)                                                       float64
Literacy rate, adult total (% of people ages 15 and above)                                    float64
Livestock production index (2004-2006 = 100)                                                  float64
Mineral rents (% of GDP)                                                                      float64
Mortality rate, infant (per 1,000 live births)                                                float64
Net migration                                                                                 float64
Permanent cropland (% of land area)                                                           float64
Population, total                                                                             float64
Rural land area (sq. km)                                                                      float64
Rural land area where elevation is below 5 meters (% of total land area)                      float64
Rural land area where elevation is below 5 meters (sq. km)                                    float64
Rural population                                                                              float64
Rural population (% of total population)                                                      float64
Rural population growth (annual %)                                                            float64
Rural population living in areas where elevation is below 5 meters (% of total population)    float64
Surface area (sq. km)                                                                         float64
dtype: object

2) 전체적인 데이터 개요 확인

wb_df[wb_df["Country Name"] == "Afghanistan"]
Country Name Country Code year Access to electricity (% of population) Access to electricity, rural (% of rural population) Agricultural irrigated land (% of total agricultural land) Agricultural land (% of land area) Agricultural land (sq. km) Agricultural machinery, tractors Agricultural machinery, tractors per 100 sq. km of arable land ... Permanent cropland (% of land area) Population, total Rural land area (sq. km) Rural land area where elevation is below 5 meters (% of total land area) Rural land area where elevation is below 5 meters (sq. km) Rural population Rural population (% of total population) Rural population growth (annual %) Rural population living in areas where elevation is below 5 meters (% of total population) Surface area (sq. km)
0 Afghanistan AFG 1960 NaN NaN NaN NaN NaN NaN NaN ... NaN 8996973.0 NaN NaN NaN 8241137.0 91.599 NaN NaN NaN
1 Afghanistan AFG 1961 NaN NaN NaN 57.745918 377000.0 120.0 0.156863 ... 0.076586 9169410.0 NaN NaN NaN 8373138.0 91.316 1.589040 NaN 652860.0
2 Afghanistan AFG 1962 NaN NaN NaN 57.837821 377600.0 150.0 0.194805 ... 0.091903 9351441.0 NaN NaN NaN 8512056.0 91.024 1.645479 NaN 652860.0
3 Afghanistan AFG 1963 NaN NaN NaN 57.914407 378100.0 200.0 0.258065 ... 0.091903 9543205.0 NaN NaN NaN 8657977.0 90.724 1.699758 NaN 652860.0
4 Afghanistan AFG 1964 NaN NaN NaN 58.010906 378730.0 200.0 0.256410 ... 0.111816 9744781.0 NaN NaN NaN 8810646.0 90.414 1.747967 NaN 652860.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
56 Afghanistan AFG 2016 97.700000 97.099360 6.48114 58.067580 379100.0 NaN NaN ... 0.277242 35383128.0 NaN NaN NaN 26530269.0 74.980 2.489324 NaN 652860.0
57 Afghanistan AFG 2017 97.700000 97.091973 NaN NaN NaN NaN NaN ... NaN 36296400.0 NaN NaN NaN 27131559.0 74.750 2.241128 NaN 652860.0
58 Afghanistan AFG 2018 98.713203 98.272872 NaN NaN NaN NaN NaN ... NaN 37172386.0 NaN NaN NaN 27695286.0 74.505 2.056463 NaN 652860.0
59 Afghanistan AFG 2019 NaN NaN NaN NaN NaN NaN NaN ... NaN 38041754.0 NaN NaN NaN 28244481.0 74.246 1.963586 NaN NaN
60 Afghanistan AFG 2020 NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

61 rows × 55 columns

각 국가별로 1960년부터 2020년까지 데이터가 주어지되, 년도마다 결측치를 갖는 컬럼이 각각 다른 것으로 보인다. 검증하고자 하는 가설에서 필요한 컬럼의 결측치가 없는 해를 골라 가설을 검증해야 할 듯 보인다.

2. 가설 수립

1) 각 컬럼 사이 상관계수 확인

wb_df.corr()["Mortality rate, infant (per 1,000 live births)"]
year                                                                                         -0.490281
Access to electricity (% of population)                                                      -0.863026
Access to electricity, rural (% of rural population)                                         -0.859266
Agricultural irrigated land (% of total agricultural land)                                    0.152809
Agricultural land (% of land area)                                                            0.064698
Agricultural land (sq. km)                                                                   -0.045362
Agricultural machinery, tractors                                                             -0.162059
Agricultural machinery, tractors per 100 sq. km of arable land                               -0.426092
Agricultural methane emissions (% of total)                                                   0.215588
Agricultural methane emissions (thousand metric tons of CO2 equivalent)                      -0.011880
Agricultural nitrous oxide emissions (% of total)                                             0.279392
Agricultural nitrous oxide emissions (thousand metric tons of CO2 equivalent)                -0.023393
Agricultural raw materials exports (% of merchandise exports)                                 0.409711
Agricultural raw materials imports (% of merchandise imports)                                 0.067556
Agriculture, forestry, and fishing, value added (% of GDP)                                    0.762580
Agriculture, forestry, and fishing, value added (current US$)                                -0.087097
Annual freshwater withdrawals, agriculture (% of total freshwater withdrawal)                 0.392959
Arable land (% of land area)                                                                 -0.080993
Arable land (hectares per person)                                                             0.087342
Arable land (hectares)                                                                       -0.028642
Average precipitation in depth (mm per year)                                                  0.021142
Birth rate, crude (per 1,000 people)                                                          0.851877
Cereal production (metric tons)                                                              -0.100151
Cereal yield (kg per hectare)                                                                -0.509914
CPIA gender equality rating (1=low to 6=high)                                                -0.452092
Crop production index (2004-2006 = 100)                                                      -0.394072
Death rate, crude (per 1,000 people)                                                          0.793594
Employment in agriculture (% of total employment) (modeled ILO estimate)                      0.802073
Employment in agriculture, female (% of female employment) (modeled ILO estimate)             0.780157
Employment in agriculture, male (% of male employment) (modeled ILO estimate)                 0.784952
Fertilizer consumption (% of fertilizer production)                                          -0.000144
Fertilizer consumption (kilograms per hectare of arable land)                                -0.129521
Food production index (2004-2006 = 100)                                                      -0.367806
Forest area (% of land area)                                                                 -0.035038
Forest area (sq. km)                                                                          0.050040
GDP per capita (current US$)                                                                 -0.432281
Land area (sq. km)                                                                           -0.047954
Land under cereal production (hectares)                                                      -0.054422
Literacy rate, adult total (% of people ages 15 and above)                                   -0.842399
Livestock production index (2004-2006 = 100)                                                 -0.351940
Mineral rents (% of GDP)                                                                      0.084642
Mortality rate, infant (per 1,000 live births)                                                1.000000
Net migration                                                                                -0.084174
Permanent cropland (% of land area)                                                          -0.066017
Population, total                                                                            -0.042794
Rural land area (sq. km)                                                                      0.085061
Rural land area where elevation is below 5 meters (% of total land area)                     -0.071699
Rural land area where elevation is below 5 meters (sq. km)                                    0.025572
Rural population                                                                             -0.008687
Rural population (% of total population)                                                      0.674677
Rural population growth (annual %)                                                            0.280045
Rural population living in areas where elevation is below 5 meters (% of total population)   -0.001724
Surface area (sq. km)                                                                        -0.048781
Name: Mortality rate, infant (per 1,000 live births), dtype: float64
plt.figure(figsize=(15, 15))
sns.heatmap(wb_df.corr())
plt.show()

2) 상관계수에 따른 가설 세우기

  • Access to electricity (% of population): 전기 사용가능 인구 비율
    • Birth rate, crude (per 1,000 people): 인구 1000명당 출생률(1년간의 총 출생아수를 당해년도의 총인구로 나눈 수치)
      • 상관관계: -0.861408
      • 전기 사용가능 인구 비율이 낮을수록 출생률이 높을 것이다.
    • CPIA gender equality rating (1=low to 6=high): CPIA(세계은행의 국가 정책·제도 평가보고서)에 따른 성평등 등급
      • 상관관계: 0.346363
      • 전기 사용가능 인구 비율이 높을수록 CPIA 성평등 등급이 높을 것이다.
    • Employment in agriculture (% of total employment) (modeled ILO estimate): 전체 노동인구 중 농업 종사자 비율
      • 상관관계; -0.830018
      • 전기 사용가능 인구 비율이 낮을수록 농업 종사자 비율이 높을 것이다.
    • GDP per capita (current US$): 1인당 GDP
      • 상관관계: 0.407089
      • 전기 사용가능 인구 비율이 높을수록 1인당 GDP가 높을 것이다.
    • Literacy rate, adult total (% of people ages 15 and above): 15세 이상 문해율
      • 상관관계: 0.828068
      • 전기 사용가능 인구 비율이 높을수록 문해율이 높을 것이다.
    • Mortality rate, infant (per 1,000 live births): 영아사망률
      • 상관관계: -0.863026
      • 전기 사용가능 인구 비율이 낮을수록 영아사망률이 높을 것이다.
    • Rural population (% of total population): 농촌인구비율
      • 상관관계: -0.675092
      • 전기 사용가능 인구 비율이 낮을수록 농촌인구비율이 높을 것이다.
  • Birth rate, crude (per 1,000 people): 인구 1000명당 출생률(1년간의 총 출생아수를 당해년도의 총인구로 나눈 수치)
    • CPIA gender equality rating (1=low to 6=high): CPIA(세계은행의 국가 정책·제도 평가보고서)에 따른 성평등 등급
      • 상관관계: -0.486804
      • 출생률이 낮을수록 CPIA 성평등 등급이 높을 것이다.
    • Employment in agriculture (% of total employment) (modeled ILO estimate): 전체 노동인구 중 농업 종사자 비율
      • 상관관계: 0.766902
      • 출생률이 높을수록 농업 종사자 비율이 높을 것이다.
    • GDP per capita (current US$): 1인당 GDP
      • 상관관계: -0.508999
      • 출생률이 낮을수록 1인당 GDP가 높을 것이다.
    • Literacy rate, adult total (% of people ages 15 and above): 15세 이상 문해율
      • 상관관계: -0.841121
      • 출생률이 낮을수록 문해율이 높을 것이다.
    • Mortality rate, infant (per 1,000 live births): 영아사망률
      • 상관관계: 0.851877
      • 출생률이 높을수록 영아사망률이 높을 것이다.
    • Rural population (% of total population): 농촌인구비율
      • 상관관계: 0.682261
      • 출생률이 높을수록 농촌인구비율이 높을 것이다.
  • CPIA gender equality rating (1=low to 6=high): CPIA(세계은행의 국가 정책·제도 평가보고서)에 따른 성평등 등급
    • Literacy rate, adult total (% of people ages 15 and above): 15세 이상 문해율
      • 상관관계: 0.488863
      • 성평등등급이 높을수록 문해율이 높을 것이다.
    • Mortality rate, infant (per 1,000 live births): 영아사망률
      • 상관관계: -0.452092
      • 성평등등급이 낮을수록 영아사망률이 높을 것이다.
  • Employment in agriculture (% of total employment) (modeled ILO estimate): 전체 노동인구 중 농업 종사자 비율
    • GDP per capita (current US$): 1인당 GDP
      • 상관관계: -0.603409
      • 농업 종사자 비율이 낮을수록 1인당 GDP가 높을 것이다.
    • Literacy rate, adult total (% of people ages 15 and above): 15세 이상 문해율
      • 상관관계: -0.750526
      • 농업 종사자 비율이 낮을수록 문해율이 높을 것이다.
    • Mortality rate, infant (per 1,000 live births): 영아사망률
      • 상관관계: 0.802073
      • 농업 종사자 비율이 높을수록 영아사망률이 높을 것이다.
    • Rural population (% of total population): 농촌인구비율
      • 상관관계: 0.799293
      • 농업 종사자 비율이 높을수록 농촌인구비율이 높을 것이다.
  • GDP per capita (current US$): 1인당 GDP
    • Literacy rate, adult total (% of people ages 15 and above): 15세 이상 문해율
      • 상관관계: 0.418514
      • 1인당 GDP가 높을수록 문해율이 높을 것이다.
    • Mortality rate, infant (per 1,000 live births): 영아사망률
      • 상관관계: -0.432281
      • 1인당 GDP가 낮을수록 영아사망률이 높을 것이다.
    • Rural population (% of total population): 농촌인구비율
      • 상관관계: -0.480503
      • 1인당 GDP가 낮을수록 농촌인구비율이 높을 것이다.
  • Literacy rate, adult total (% of people ages 15 and above): 15세 이상 문해율
    • Mortality rate, infant (per 1,000 live births): 영아사망률
      • 상관관계: -0.842399
      • 문해율이 낮을수록 영아사망률이 높을 것이다.
    • Rural population (% of total population): 농촌인구비율
      • 상관관계: -0.660029
      • 문해율이 낮을수록 농촌인구비율이 높을 것이다.
  • Mortality rate, infant (per 1,000 live births): 영아사망률
    • Rural population (% of total population): 농촌인구비율
      • 상관관계: 0.674677
      • 영아사망률이 높을수록 농촌인구비율이 높을 것이다.

다음과 같이 총 8개의 컬럼 사이에 0.3 이상 양 또는 음의 상관관계가 있음을 확인했다.

  • Access to electricity (% of population)
  • Birth rate, crude (per 1,000 people)
  • CPIA gender equality rating (1=low to 6=high)
  • Employment in agriculture (% of total employment) (modeled ILO estimate)
  • GDP per capita (current US$)
  • Literacy rate, adult total (% of people ages 15 and above)
  • Mortality rate, infant (per 1,000 live births)
  • Rural population (% of total population)
cutted_df = wb_df[["Country Name", "year", "Access to electricity (% of population)", "Birth rate, crude (per 1,000 people)", "CPIA gender equality rating (1=low to 6=high)", "Employment in agriculture (% of total employment) (modeled ILO estimate)", "GDP per capita (current US$)", "Literacy rate, adult total (% of people ages 15 and above)", "Mortality rate, infant (per 1,000 live births)", "Rural population (% of total population)","Population, total"]]
cutted_df = cutted_df.sort_values("year", ascending=True)

3. 가설 검증

1) 농업종사자 비율이 낮을수록 1인당 GDP가 높을 것이다

  • 먼저, 결측치를 제거한다.
nadropped_df = cutted_df[(cutted_df["GDP per capita (current US$)"].isna() == False) & (cutted_df["Employment in agriculture (% of total employment) (modeled ILO estimate)"].isna()==False) & (cutted_df["Population, total"].isna()==False)]
  • 년도별로 변화를 관찰할 수 있는 그래프를 그린다.
fig = px.scatter(nadropped_df,x="Employment in agriculture (% of total employment) (modeled ILO estimate)", y="GDP per capita (current US$)",
                 animation_frame="year", size="Population, total", animation_group="Country Name", hover_name="Country Name", size_max=60,
                 range_x=[0, 100], range_y=[100, 130000], log_y=True
 )
fig.show()

년도를 가리지 않고 농업종사자 비율이 낮을수록 1인당 GDP가 높은 경향이 나타나는 것을 확인할 수 있다.

2) 문해율이 낮을수록 영아사망률이 높을 것이다.

  • 먼저, 결측치를 제거한다.
nadropped_df = cutted_df[(cutted_df["Literacy rate, adult total (% of people ages 15 and above)"].isna() == False) & (cutted_df["Mortality rate, infant (per 1,000 live births)"].isna()==False) & (cutted_df["Population, total"].isna()==False)]
nadropped_df = nadropped_df[["year", "Country Name", "Literacy rate, adult total (% of people ages 15 and above)", "Mortality rate, infant (per 1,000 live births)", "Population, total"]]
nadropped_df.head(5)
year Country Name Literacy rate, adult total (% of people ages 15 and above) Mortality rate, infant (per 1,000 live births) Population, total
5561 1970 Grenada 97.794472 39.9 94475.0
1047 1970 Barbados 99.268082 40.9 238896.0
13796 1970 St. Vincent and the Grenadines 95.632156 54.2 90509.0
2394 1975 Central African Republic 18.236170 126.7 1958367.0
15448 1975 Uruguay 93.861069 49.0 2830176.0
  • 위 데이터의 경우 어떤 해는 3개 국가의 지표만 존재하여 경향을 확인하기 어려운 문제가 있다. 이를 위하여 데이터에서 각 년도별로 데이터가 20개 이하인 해는 아예 연도 데이터를 삭제한다.
nadropped_df.groupby("year").count()
Country Name Literacy rate, adult total (% of people ages 15 and above) Mortality rate, infant (per 1,000 live births) Population, total
year
1970 3 3 3 3
1975 6 6 6 6
1976 7 7 7 7
1977 1 1 1 1
1978 2 2 2 2
1979 6 6 6 6
1980 16 16 16 16
1981 17 17 17 17
1982 7 7 7 7
1983 2 2 2 2
1984 8 8 8 8
1985 6 6 6 6
1986 4 4 4 4
1987 3 3 3 3
1988 5 5 5 5
1989 10 10 10 10
1990 52 52 52 52
1991 61 61 61 61
1992 46 46 46 46
1993 42 42 42 42
1994 50 50 50 50
1995 44 44 44 44
1996 48 48 48 48
1997 42 42 42 42
1998 45 45 45 45
1999 48 48 48 48
2000 83 83 83 83
2001 69 69 69 69
2002 59 59 59 59
2003 51 51 51 51
2004 64 64 64 64
2005 61 61 61 61
2006 67 67 67 67
2007 75 75 75 75
2008 73 73 73 73
2009 74 74 74 74
2010 90 90 90 90
2011 97 97 97 97
2012 85 85 85 85
2013 73 73 73 73
2014 91 91 91 91
2015 81 81 81 81
2016 73 73 73 73
2017 69 69 69 69
2018 114 114 114 114
2019 37 37 37 37
  • 1990년부터 그래프를 그리는 것이 적절한 듯 보인다.
nadropped_df = nadropped_df[nadropped_df["year"] >= 1990]
  • 년도별로 변화를 관찰할 수 있는 그래프를 그린다.
fig = px.scatter(nadropped_df,y="Mortality rate, infant (per 1,000 live births)", x="Literacy rate, adult total (% of people ages 15 and above)",
                 animation_frame="year",  size="Population, total", animation_group="Country Name", hover_name="Country Name", size_max=30,
                 range_x=[0, 100], range_y=[0, 200]
 )
fig.show()

년도를 가리지 않고 문해율이 낮을수록 엳아사망률이 높은 경향이 나타나는 것을 확인할 수 있다.

3) 출생률이 낮을수록 성평등 등급이 높을 것이다.

  • 먼저, 결측치를 제거한다.
nadropped_df = cutted_df[(cutted_df["Birth rate, crude (per 1,000 people)"].isna() == False) & (cutted_df["CPIA gender equality rating (1=low to 6=high)"].isna()==False)& (cutted_df["Population, total"].isna()==False)]
  • 년도별로 변화를 관찰할 수 있는 그래프를 그린다.
fig = px.scatter(nadropped_df,y="CPIA gender equality rating (1=low to 6=high)", x="Birth rate, crude (per 1,000 people)",
                 animation_frame="year", animation_group="Country Name", size="Population, total",hover_name="Country Name", size_max=30,
                 range_x=[5, 60], range_y=[0, 7]
 )
fig.show()

년도를 가리지 않고 출생률이 낮을수록 성평등 등급이 높은 경향이 나타나지만 그 경향이 그리 강하지는 않음을 확인할 수 있다.